home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C16 / Stackt.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1007 b   |  45 lines

  1. //: C16:Stackt.h
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Simple stack template
  7. #ifndef STACKT_H
  8. #define STACKT_H
  9. template<class T> class StacktIter; // Declare
  10.  
  11. template<class T>
  12. class Stackt {
  13.   static const int ssize = 100;
  14.   T stack[ssize];
  15.   int top;
  16. public:
  17.   Stackt() : top(0) { stack[top] = 0; }
  18.   void push(const T& i) {
  19.     if(top < ssize) stack[top++] = i;
  20.   }
  21.   T pop() {
  22.     return stack[top > 0 ? --top : top];
  23.   }
  24.   friend class StacktIter<T>;
  25. };
  26.  
  27. template<class T>
  28. class StacktIter {
  29.   Stackt<T>& s;
  30.   int index;
  31. public:
  32.   StacktIter(Stackt<T>& is)
  33.     : s(is), index(0) {}
  34.   T& operator++() { // Prefix form
  35.     if (index < s.top - 1) index++;
  36.     return s.stack[index];
  37.   }
  38.   T& operator++(int) { // Postfix form
  39.     int returnIndex = index;
  40.     if (index < s.top - 1) index++;
  41.     return s.stack[returnIndex];
  42.   }
  43. };
  44. #endif // STACKT_H ///:~
  45.